﻿Option Explicit On
Option Strict On

Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlServerCe

Imports System.Xml
Imports System.Data.OleDb
Imports System.Text
Imports System.Security.Cryptography



Public Class Form1

    Private personalController As New PersonalController()


    Private Sub onLoad1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'txtXml.Text = "D:\temp\Personal\Personal.xml"
        habilitarBotones(False)

        Text = Text & " v1.0"
        Dim lista As New ArrayList

        Dim fechaActual As Date = Date.Now()
        Dim anhoActual As String = fechaActual.ToString("yyyy") 'ddMMMyyyy

        Dim p = -1
        Dim i = 0
        For a As Integer = 2014 To 2030
            lista.Add(a)

            Dim aString As String = a & ""
            If aString = anhoActual Then
                p = i
            End If

            i = i + 1
        Next

        cboAnho.Items.AddRange(lista.ToArray())
        cboAnho.SelectedIndex = p

        'MsgBox(Convert.ToString(cboAnho.SelectedItem))

    End Sub

    Private Sub habilitarBotones(ByVal habilitar As Boolean)
        btnSdf.Enabled = habilitar
        btnMdb.Enabled = habilitar
        btnDat.Enabled = habilitar
        btnExportar.Enabled = habilitar
    End Sub

    Private Sub btnXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXml.Click
        'test1()
        'Return

        OpenFileDialog1.FileName = "Personal"
        OpenFileDialog1.Filter = "Archivos XML (*.xml)|*.xml;Personal*"
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            txtXml.Text = OpenFileDialog1.FileName
            habilitarBotones(True)
            'test(OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub test1()
        Dim crip As New CriptoTripleDES("123")
        Dim cripto As String = crip.encrypt("<a> Ñ")
        Dim descripto As String = crip.decrypt(cripto)
        MsgBox(cripto)
        MsgBox(descripto)
        MsgBox(crip.decrypt(ArchivoUtil.getArchivoComoTexto("d:/cripto.txt")))
    End Sub

    Private Sub test(ByVal archivoXML As String)


        Dim crip As New CriptoTripleDES("123")
        Dim data As String = crip.encrypt(ArchivoUtil.getArchivoComoTextoUTF8(archivoXML))
        ArchivoUtil.copiarArchivo(archivoXML, "d:/copia.xml")
        ArchivoUtil.escribirArchivo("d:/cripto_test.txt", data)
        data = crip.decrypt(ArchivoUtil.getArchivoComoTextoUTF8("d:/prueba.txt"))
        ArchivoUtil.escribirArchivo("d:/cripto_test_decrypt.xml", data)
    End Sub

    Private Sub btnSdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSdf.Click
        SaveFileDialog1.FileName = "Personal.sdf"
        SaveFileDialog1.Filter = "Archivos SDF (*.sdf)|*.sdf"
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            'MsgBox(SaveFileDialog1.FileName)
            personalController.crearSDF(txtXml.Text, SaveFileDialog1.FileName, True)
            'My.Computer.FileSystem.WriteAllText _
            '(SaveFileDialog1.FileName, RichTextBox1.Text, True)
        End If

    End Sub


    Private Sub btnMdb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMdb.Click
        SaveFileDialog1.FileName = "Personal.mdb"
        SaveFileDialog1.Filter = "Archivos MDB (*.mdb)|*.mdb"
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            personalController.crearMDB(txtXml.Text, SaveFileDialog1.FileName, True)
        End If
    End Sub

    Private Sub btnDat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDat.Click
        SaveFileDialog1.FileName = "Personal.dat"
        SaveFileDialog1.Filter = "Archivos DAT (*.dat)|*.dat"
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            personalController.crearDAT(txtXml.Text, SaveFileDialog1.FileName, True)
        End If

    End Sub

    Private Sub btnExportar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportar.Click
        If cboAnho.SelectedIndex < 0 Then
            MsgBox("Debe seleccionar un año")
            Return
        End If

        Dim anho As String = Convert.ToString(cboAnho.SelectedItem)

        Dim dirPBasico = "C:\Enaho" & anho & "\PBasico"
        If Not System.IO.Directory.Exists(dirPBasico) Then
            MsgBox("No se encontró el directorio '" & dirPBasico & "'. Falta instalar el PBásico")
            Return
        End If

        txtSdf.Text = "NO EXPORTADO"
        txtMdb.Text = "NO EXPORTADO"
        txtDat.Text = "NO EXPORTADO"

        Dim dirPDA = dirPBasico & "\PDA"
        If Not System.IO.Directory.Exists(dirPDA) Then
            System.IO.Directory.CreateDirectory(dirPDA)
        End If

        Dim sdf As String = dirPDA & "\Personal.sdf"
        'Dim mdb As String = dirPBasico & "\personal2011.mdb"
        Dim mdb As String = dirPBasico & "\Personal.mdb" 'este es el archivo q lee el PBASICO
        Dim dat As String = dirPBasico & "\Tab\Perso034.dat"

        If personalController.crearSDF(txtXml.Text, sdf, False) Then
            txtSdf.Text = sdf
        End If

        If personalController.crearMDB(txtXml.Text, mdb, False) Then
            txtMdb.Text = mdb
        End If

        If personalController.crearDAT(txtXml.Text, dat, False) Then
            txtDat.Text = dat
        End If

        MsgBox("Fin de la exportación")
    End Sub
End Class
